package com.example.xunua_weather;

import android.content.DialogInterface;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;


import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.xunua_weather.Adapter.DeleteCityAdapter;
import com.example.xunua_weather.Sqliteutils.DBManager;

import java.util.ArrayList;
import java.util.List;

public class DeleteCityActivity extends AppCompatActivity implements OnClickListener{
     ImageView errorIv,rightIv;
     ListView deleteLv;
     List<String> cityList;
    private DeleteCityAdapter adapter;
    List<String>deleteCityList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delete_city);
        errorIv = findViewById(R.id.city_error_iv);
        rightIv = findViewById(R.id.city_right_iv);
        deleteLv = findViewById(R.id.city_delete_lv);
        errorIv.setOnClickListener(this);
        rightIv.setOnClickListener(this);
        //获取数据库当中所有城市的名称，将城市名称显示在集合中
        cityList=DBManager.queryAllCityName();
        deleteCityList=new ArrayList<>();
        adapter=new DeleteCityAdapter(this,cityList,deleteCityList);
        deleteLv.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.city_error_iv:
                AlertDialog.Builder builder=new AlertDialog.Builder(this);
                builder.setMessage("您确定要舍弃更改么？");
                builder.setPositiveButton("舍弃更改", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                }).setNegativeButton("取消",null);
                break;
            case R.id.city_right_iv:
                for (int i = 0; i < deleteCityList.size(); i++) {
                    String deleCity=deleteCityList.get(i);
                    //通过城市名字 从SqLite中删除
                    DBManager.deleteInfoBycity(deleCity);
                }
                //删除完返回上一级页面
                finish();
                break;
        }
    }
}
